/**
 * 版权所有 ©2013 威海简单软件科技有限公司 保留所有权利
 * 工程名：base
 * 文件名：WordExport.java
 * 创建日期：2013-10-24 上午10:22:23
 * 作者：张世鹏
 */
package com.whxd.framework.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;

/**
 * 导出word辅助类
 * 
 * @author 张世鹏
 * @since 2013-10-24
 */
public class WordExport {

	private String xmlContent;// 读取的数据

	/**
	 * 有参构造
	 * 
	 * @author 张世鹏
	 * @since 2013-10-24
	 * @param readPath
	 * @param writePath
	 * @throws IOException
	 */
	public WordExport(String readPath) throws IOException {
		super();
		this.xmlContent = read(readPath);
	}

	/**
	 * 读取文件内容
	 * 
	 * @author 张世鹏
	 * @since 2013-10-24
	 * @param readPath
	 *            ：读取路径
	 * @return
	 * @throws IOException
	 */
	private String read(String readPath) throws IOException {
		BufferedReader reader = null;
		InputStreamReader isr = null;
		String tmpCollet = "";
		try {
			if (null != "utf-8") {
				isr = new InputStreamReader(new FileInputStream(readPath), "utf-8");
			} else {
				isr = new InputStreamReader(new FileInputStream(readPath));
			}
			reader = new BufferedReader(isr);
			String tmp = reader.readLine();
			while (null != tmp) {
				tmpCollet += tmp;
				tmp = reader.readLine();
			}
		} finally {
			if (null != reader) {
				reader.close();
			}
		}
		return tmpCollet;
	}

	/**
	 * 生成导出文件
	 * 
	 * @author 张世鹏
	 * @since 2013-10-24
	 * @param response
	 * @param filename
	 *            :生成的文件名
	 * @throws IOException
	 */
	public void export(HttpServletRequest request, HttpServletResponse response, String filename) throws IOException {
		if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
			filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
		} else {
			filename = URLEncoder.encode(filename, "UTF-8");
		}
		response.addHeader("Content-Disposition", "attachment;filename=" + filename);
		response.setContentType("application/octet-stream");
		OutputStreamWriter writer = null;
		try {
			if (null != "utf-8") {
				writer = new OutputStreamWriter(response.getOutputStream(), "utf-8");
			} else {
				writer = new OutputStreamWriter(response.getOutputStream());
			}
			writer.append(xmlContent);
		} finally {
			if (null != writer) {
				writer.flush();
				writer.close();
			}
		}
	}

	/**
	 * 字符串替换
	 * 
	 * @author 张世鹏
	 * @since 2013-10-24
	 * @param rFix
	 *            ：被替换的字符
	 * @param rRep
	 *            ：替换字符
	 * @return
	 */
	public String fillText(String rFix, String rRep) {
		int l = 0;
		String gRtnStr = xmlContent;
		do {
			l = xmlContent.indexOf(rFix, l);
			if (l == -1)
				break;
			gRtnStr = xmlContent.substring(0, l) + rRep + xmlContent.substring(l + rFix.length());
			l += rRep.length();
			xmlContent = gRtnStr;
		} while (true);
		return gRtnStr.substring(0, gRtnStr.length());
	}

	/**
	 * 图片的替换
	 * 
	 * @author 张世鹏
	 * @since 2013-10-24
	 * @param rFix
	 *            :被替换的字符
	 * @param picUrl
	 *            :图片的路径
	 * @return
	 */
	public String fillPic(String rFix, String picUrl) throws IOException {
		String imageSrc = GetImageStr(picUrl);
		return fillText(rFix, imageSrc);
	}

	// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
	private String GetImageStr(String imgFile) throws IOException {
		InputStream in = null;
		byte[] data = null;
		// 读取图片字节数组
		in = new FileInputStream(imgFile);
		data = new byte[in.available()];
		in.read(data);
		in.close();
		// 对字节数组Base64编码

		// BASE64Encoder encoder = new BASE64Encoder();
		// return encoder.encode(data);// 返回Base64编码过的字节数组字符串

		// TODO 修改encode兼容1.6和1.7@张世鹏
		return Base64.encodeBase64String(data);
	}
}
